package com.jerry.hm.algorithm.recursion;

import java.util.LinkedList;

/**
 * 汉诺塔
 *
 * @author Jerry
 * @since 2024/10/1 17:16
 */
public class HanoiTower {

    private static LinkedList<Integer> one = new LinkedList<>();
    private static LinkedList<Integer> two = new LinkedList<>();
    private static LinkedList<Integer> three = new LinkedList<>();

    /**
     * 汉诺塔移动
     *
     * @param n 圆盘个数
     * @param a 初始柱子
     * @param b 借助柱子
     * @param c 目标柱子
     */
    public static void move(int n, LinkedList<Integer> a, LinkedList<Integer> b, LinkedList<Integer> c) {
        if (n == 0) {
            return;
        }
        move(n - 1, a, c, b);
        c.addLast(a.removeLast());
        print();
        move(n - 1, b, a, c);
    }

    private static void init(int n) {
        for (int i = n; i > 0; i--) {
            one.addLast(i);
        }
    }

    private static void print() {
        System.out.println("---");
        System.out.println(one);
        System.out.println(two);
        System.out.println(three);
    }

    public static void main(String[] args) {
        init(5);
        print();
        move(5, one, two, three);
    }
}
